package com.forlove.app.common.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.Toast;

import com.forlove.app.ForloveApplication;
import com.forlove.app.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.File;

/**
 * Created by yinzhiqiang on 2017/11/1.
 */

public class ShareUtils {

    //获取分享结果的回调
    public interface ShareResultListener {
        void shareResult(SHARE_MEDIA platform, int result);
    }

    private static final String UNRegisterURL = "https://t.growingio.com/app/at0/DnRbMz93";
    private static UMWeb web;
    private static UMImage image;
    private static Bitmap imageBitmap;
    private static ShareResultListener shareListener;
    public static final int SHARE_SUCCESS = 1;//成功
    public static final int SHARE_CANCEL = 2;//取消
    public static final int SHARE_ERROR = 3;//错误
    public static final int SHARE_START = 4;//开始

    public static void serShareResultListener(ShareResultListener shareResultListener) {
        shareListener = shareResultListener;
    }

    public static void toSharedWX(SHARE_MEDIA media, Activity activity,
                                  String url, String title, String sharDescription) {
        imageBitmap = BitmapFactory.decodeResource(
                activity.getResources(), R.mipmap.icon_share);
        image = new UMImage(activity, imageBitmap);
        if (TextUtils.isEmpty(url)) {
            url = UNRegisterURL;
        }
        setUMWeb(title, url, image,
                sharDescription);
        if (media.equals(SHARE_MEDIA.SINA)) {
            new ShareAction(activity)
                    .setPlatform(media)
                    .setCallback(umShareListener)
                    .withMedia(web)
                    .share();
        } else if (media.equals(SHARE_MEDIA.SMS)) {
            sendSMS(title + url, activity);
        } else {
            new ShareAction(activity)
                    .setPlatform(media)
                    .setCallback(umShareListener)
                    .withMedia(web)
                    .share();
        }
    }

    /**
     * 只分享图片。不支持短信
     *
     * @param media
     * @param activity
     * @param path     图片路径
     */
    public static void toShareImage(SHARE_MEDIA media, Activity activity, String path, UMShareListener shareListener) {
        image = new UMImage(activity, path);
        image.compressStyle = UMImage.CompressStyle.SCALE;
        toShareImage(media, activity, image, shareListener);
    }

    /**
     * 分享图片
     *
     * @param media
     * @param activity
     * @param bitmap
     */
    public static void toShareImage(SHARE_MEDIA media, Activity activity, Bitmap bitmap, UMShareListener shareListener) {
        image = new UMImage(activity, bitmap);
        image.compressStyle = UMImage.CompressStyle.SCALE;
        toShareImage(media, activity, image, shareListener);
    }

    public static void toShareImage(SHARE_MEDIA media, Activity activity, UMImage umImage, UMShareListener shareListener) {

        if (media.equals(SHARE_MEDIA.SINA)) {
            new ShareAction(activity)
                    .setPlatform(media)
                    .withMedia(umImage)
                    .setCallback(shareListener)
                    .share();
        } else {
            new ShareAction(activity)
                    .setPlatform(media)
                    .withMedia(umImage)
                    .setCallback(shareListener)
                    .share();
        }
    }

    private static void sendSMS(String smsBody, Context context) {
        Uri smsToUri = Uri.parse("smsto:");
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", smsBody);
        context.startActivity(intent);

    }

    private static void setUMWeb(String title, String url, UMImage image, String desc) {
        image.compressStyle = UMImage.CompressStyle.SCALE;
        web = new UMWeb(url);
        web.setTitle(title);
        web.setThumb(image);
        web.setDescription(desc);
    }


    @SuppressLint("NewApi")
    public static void copy(String content, Context context) {
        // 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(content.trim());
    }

    private static UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            if (shareListener != null) {
                shareListener.shareResult(share_media, SHARE_START);
            }
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (shareListener != null) {
                shareListener.shareResult(platform, SHARE_SUCCESS);
            }
            Toast.makeText(ForloveApplication.getInstance(), "邀请成功!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (t.toString().contains("2008")) {
                Toast.makeText(ForloveApplication.getInstance(), "没有安装相关应用", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(ForloveApplication.getInstance(), "取消邀请圈一圈至" + getMedia(platform) + "失败!", Toast.LENGTH_SHORT).show();
            }
            if (shareListener != null) {
                shareListener.shareResult(platform, SHARE_ERROR);
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(ForloveApplication.getInstance(), "取消分享圈一圈至" + getMedia(platform) + "!", Toast.LENGTH_SHORT).show();
            if (shareListener != null) {
                shareListener.shareResult(platform, SHARE_CANCEL);
            }
        }
    };

    public static String getMedia(SHARE_MEDIA platform) {
        String media = "";
        switch (platform) {
            case SINA:
                media = "新浪微博";
                break;
            case QQ:
                media = "QQ好友";
                break;
            case QZONE:
                media = "QQ空间";
                break;
            case WEIXIN:
                media = "微信好友";
                break;
            case WEIXIN_CIRCLE:
                media = "微信朋友圈";
                break;
            default:
                break;
        }
        return media;
    }


    private static String getFileRoot(Context context) {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            File external = context.getExternalFilesDir(null);
            if (external != null) {
                return external.getAbsolutePath();
            }
        }

        return context.getFilesDir().getAbsolutePath();
    }

    /**
     * 分享结束不再使用资源时
     */
    public static void clearShare(Context context) {
        if (shareListener != null) {
            shareListener = null;
        }
        if (imageBitmap != null) {
            imageBitmap.recycle();
        }
        UMShareAPI.get(context).release();
    }

}
